<template>
  <div id="panel">
    <!-- 搜索表单区域 -->
    <el-card class="queryForm">
      <el-form
        ref="queryForm"
        :model="queryParams"
        :inline="true"
        :label-position="'right'"
      >
        <el-form-item label="姓名：" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="单位：" prop="unit">
          <el-select v-model="queryParams.unit" placeholder="请选择单位">
            <el-option
              label="嵩县环境保护局"
              value="嵩县环境保护局"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应急组织机构：" prop="org">
          <el-select v-model="queryParams.org" placeholder="请选择应急组织机构">
            <el-option
              label="应急指挥部办公室"
              value="应急指挥部办公室"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应急职务：" prop="emergencyDuty">
          <el-select
            v-model="queryParams.emergencyDuty"
            placeholder="请选择应急职务"
          >
            <el-option label="队长" value="队长"></el-option>
            <el-option label="局长" value="局长"></el-option>
            <el-option label="主任" value="主任"></el-option>
          </el-select>
        </el-form-item>
        <div class="searchBtnBox">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button @click="resetQuery">重置</el-button>
        </div>
      </el-form>
    </el-card>

    <!-- 操作按钮和表格区域 -->
    <el-card class="pageContent">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdate"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="tableData"
        stripe
        :height="tableHeight"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="contact" label="联系方式" />
        <el-table-column prop="region" label="行政区划" />
        <el-table-column prop="unit" label="单位" />
        <el-table-column prop="org" label="组织机构" />
        <el-table-column prop="emergencyDuty" label="应急职务" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="view(scope.row)">详情</el-button>
            <el-button type="text" @click="edit(scope.row)">修改</el-button>
            <el-button type="text" @click="delRows(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination-self"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :pager-count="5"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        layout="slot, total, sizes, prev, pager, next, jumper"
        :total="tableList.total"
      >
        <el-button
          class="refresh-button"
          icon="el-icon-refresh"
          title="刷新"
          @click="getList"
        />
      </el-pagination>
    </el-card>

    <!-- 新增/修改弹框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="行政区划" prop="region" required>
              <el-select v-model="form.region" placeholder="请选择行政区划">
                <el-option label="嵩县" value="嵩县"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="姓名" prop="name" required>
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="办公室电话" prop="officeTel">
              <el-input
                v-model="form.officeTel"
                placeholder="请输入办公室电话"
              />
            </el-form-item>
            <el-form-item label="应急组织机构" prop="org">
              <el-select v-model="form.org" placeholder="请选择应急组织机构">
                <el-option
                  label="应急指挥部办公室"
                  value="应急指挥部办公室"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="门牌号" prop="houseNumber">
              <el-input v-model="form.houseNumber" placeholder="请输入门牌号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit" required>
              <el-select v-model="form.unit" placeholder="请选择单位">
                <el-option
                  label="嵩县环境保护局"
                  value="嵩县环境保护局"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="职务" prop="duty">
              <el-input v-model="form.duty" placeholder="请输入职务" />
            </el-form-item>
            <el-form-item label="联系方式" prop="contact" required>
              <el-input v-model="form.contact" placeholder="请输入联系方式" />
            </el-form-item>
            <el-form-item label="应急职务" prop="emergencyDuty" required>
              <el-select
                v-model="form.emergencyDuty"
                placeholder="请选择应急职务"
              >
                <el-option label="队长" value="队长"></el-option>
                <el-option label="局长" value="局长"></el-option>
                <el-option label="主任" value="主任"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "EmergencyPerson",
  components: {},
  data() {
    return {
      tableHeight: window.innerHeight - 400,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "", // 姓名
        unit: "", // 单位
        org: "", // 应急组织机构
        emergencyDuty: "", // 应急职务
      },
      tableList: {
        total: 0,
        rows: [],
      },
      ids: [], // 选中项ID集合
      tableData: [
        {
          id: "1",
          name: "李高平",
          contact: "*******",
          region: "高平",
          unit: "高平市环境保护局",
          org: "应急指挥部办公室",
          emergencyDuty: "队长",
          remark: "",
        },
        {
          id: "2",
          name: "王晋城",
          contact: "*******",
          region: "高平",
          unit: "高平市环境保护局",
          org: "应急指挥部办公室",
          emergencyDuty: "局长",
          remark: "",
        },
        {
          id: "3",
          name: "赵丹河",
          contact: "*******",
          region: "高平",
          unit: "高平市环境保护局",
          org: "应急指挥部办公室",
          emergencyDuty: "主任",
          remark: "",
        },
        {
          id: "4",
          name: "管理员",
          contact: "*******",
          region: "高平",
          unit: "高平市环境保护局",
          org: "应急指挥部办公室",
          emergencyDuty: "主任",
          remark: "",
        },
      ],
      open: false, // 弹框显隐
      title: "", // 弹框标题
      form: {
        id: "",
        region: "", // 行政区划
        name: "", // 姓名
        officeTel: "", // 办公室电话
        org: "", // 应急组织机构
        houseNumber: "", // 门牌号
        unit: "", // 单位
        duty: "", // 职务
        contact: "", // 联系方式
        emergencyDuty: "", // 应急职务
        remark: "", // 备注
      },
      rules: {
        region: [
          { required: true, message: "行政区划不能为空", trigger: "change" },
        ],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        unit: [{ required: true, message: "单位不能为空", trigger: "change" }],
        contact: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
          {
            pattern: /^1\d{10}$/,
            message: "请输入正确的手机号格式",
            trigger: "blur",
          },
        ],
        emergencyDuty: [
          { required: true, message: "应急职务不能为空", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.pageInit();
  },
  methods: {
    pageInit() {
      this.getList();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员信息";
    },
    handleUpdate() {
      if (this.ids.length === 0) {
        this.$message.warning("请先勾选需要修改的数据");
        return;
      }
      if (this.ids.length > 1) {
        this.$message.warning("只能选择一条数据进行修改");
        return;
      }
      const row = this.tableData.find((item) => item.id === this.ids[0]);
      this.reset();
      this.open = true;
      this.title = "修改人员信息";
      this.form = { ...row };
    },
    handleDelete() {
      if (this.ids.length === 0) {
        this.$message.warning("请先勾选需要删除的数据");
        return;
      }
      const names = this.tableData
        .filter((item) => this.ids.includes(item.id))
        .map((item) => item.name)
        .join("、");
      this.$confirm(`是否确认删除以下人员：${names}`, "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.tableData = this.tableData.filter(
            (item) => !this.ids.includes(item.id)
          );
          this.ids = [];
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
    view(row) {
      console.log("查看详情", row);
    },
    edit(row) {
      this.reset();
      this.open = true;
      this.title = "修改人员信息";
      this.form = { ...row };
    },
    delRows(row) {
      this.$confirm(`是否确认删除人员：${row.name}`, "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.tableData = this.tableData.filter((item) => item.id !== row.id);
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.getList();
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: "",
        region: "",
        name: "",
        officeTel: "",
        org: "",
        houseNumber: "",
        unit: "",
        duty: "",
        contact: "",
        emergencyDuty: "",
        remark: "",
      };
      this.$refs.form && this.$refs.form.resetFields();
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.open = false;
            this.$message.success("修改成功");
          } else {
            this.form.id = Date.now(); // 模拟生成ID
            this.tableData.unshift(this.form);
            this.open = false;
            this.$message.success("新增成功");
          }
          this.getList();
        }
      });
    },
    handleExport() {
      this.$message.success("导出功能待实现");
    },
    getList() {
      this.tableList.total = this.tableData.length;
      this.tableList.rows = this.tableData;
    },
  },
};
</script>
<style lang="scss" scoped>
p {
  margin: 0;
}

img {
  vertical-align: middle;
}

#panel {
  padding-bottom: 40px;
  min-height: calc(100% - 20px);
  margin: 0;

  .pagination-self {
    margin: 20px 0;
    display: flex;
    justify-content: flex-end;
    position: relative;

    .el-pagination__total {
      position: absolute;
      left: 5px;
      color: #409eff;
      font-weight: 600;
      font-size: 15px;
    }
  }

  .refresh-button {
    .el-icon-refresh {
      font-size: 18px;
      line-height: 28px;
    }
  }

  .el-card {
    margin: 10px;
  }

  .queryForm .el-card__body {
    padding: 0 20px !important;
  }

  .queryForm {
    .el-form-item {
      margin: 10px 0 10px 20px;
    }

    .el-form-item:first-child {
      margin-left: 0 !important;
    }

    .el-form-item__label {
      padding-right: 5px;
      color: #555;
    }

    .times {
      .el-form-item__content {
        width: 250px;
      }
    }

    .el-form-item__content {
      min-width: 200px;
    }

    .searchBtnBox {
      display: inline-block;
      margin: 10px 0 10px 20px;
    }
  }

  .pageContent {
    .tabs-list {
      padding-top: 20px;
    }

    .el-tabs__header {
      background: #fff;
    }

    .el-tabs__item {
      border: 1px solid #eee;
      margin: 0;
      border-left: 0;
      background: #fafbfc;
      color: #000;
    }

    .el-tabs__item:first-child {
      margin-left: 20px;
      border-left: 1px solid #eee;
    }

    .is-active {
      border-bottom: 0 !important;
      background: #fff;
      color: #307eff;
    }
  }

  .anjian label {
    padding: 0 !important;
    width: 100px !important;
  }

  .queryForm .el-checkbox {
    margin-right: 10px !important;
    width: auto !important;
  }

  .iszgfc .el-form-item__content {
    width: 120px;
  }
}
</style>
